package douyin

import (
	"douhu_backend/internal/controller/common"
	douyinService "douhu_backend/internal/services/douyin"
	"strconv"

	"github.com/kataras/iris/v12"
)

type Douyin struct {
	Base
	douyinService *douyinService.Service
}

func NewDouyinController(douyinSvc *douyinService.Service) *Douyin {
	return &Douyin{
		douyinService: douyinSvc,
	}
}

// Profile APIs
func (dy *Douyin) CreateProfile(ctx iris.Context) {
	var req douyinService.CreateProfileRequest
	if err := ctx.ReadJSON(&req); err != nil {
		dy.Error(ctx, ErrParams.Code, ErrParams.Message, err)
		return
	}

	profile, err := dy.douyinService.CreateProfile(&req)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, profile)
}

func (dy *Douyin) GetProfile(ctx iris.Context) {
	id := ctx.Params().Get("id")
	if id == "" {
		dy.Error(ctx, ErrParams.Code, "Profile ID is required", nil)
		return
	}

	profile, err := dy.douyinService.GetProfile(id)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, profile)
}

func (dy *Douyin) GetProfiles(ctx iris.Context) {
	pageStr := ctx.URLParamDefault("page", "1")
	pageSizeStr := ctx.URLParamDefault("page_size", "10")

	page, err := strconv.ParseInt(pageStr, 10, 64)
	if err != nil {
		page = 1
	}

	pageSize, err := strconv.ParseInt(pageSizeStr, 10, 64)
	if err != nil {
		pageSize = 10
	}

	result, err := dy.douyinService.GetProfiles(page, pageSize)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, result)
}

func (dy *Douyin) UpdateProfile(ctx iris.Context) {
	id := ctx.Params().Get("id")
	if id == "" {
		dy.Error(ctx, ErrParams.Code, "Profile ID is required", nil)
		return
	}

	var req douyinService.UpdateProfileRequest
	if err := ctx.ReadJSON(&req); err != nil {
		dy.Error(ctx, ErrParams.Code, ErrParams.Message, err)
		return
	}

	profile, err := dy.douyinService.UpdateProfile(id, &req)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, profile)
}

func (dy *Douyin) DeleteProfile(ctx iris.Context) {
	id := ctx.Params().Get("id")
	if id == "" {
		dy.Error(ctx, ErrParams.Code, "Profile ID is required", nil)
		return
	}

	err := dy.douyinService.DeleteProfile(id)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, common.RspProto{
		Code:    OK.Code,
		Message: "Profile deleted successfully",
	})
}

// Video APIs
func (dy *Douyin) CreateVideo(ctx iris.Context) {
	var req douyinService.CreateVideoRequest
	if err := ctx.ReadJSON(&req); err != nil {
		dy.Error(ctx, ErrParams.Code, ErrParams.Message, err)
		return
	}

	video, err := dy.douyinService.CreateVideo(&req)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, video)
}

func (dy *Douyin) GetVideo(ctx iris.Context) {
	id := ctx.Params().Get("id")
	if id == "" {
		dy.Error(ctx, ErrParams.Code, "Video ID is required", nil)
		return
	}

	video, err := dy.douyinService.GetVideo(id)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, video)
}

func (dy *Douyin) GetVideos(ctx iris.Context) {
	pageStr := ctx.URLParamDefault("page", "1")
	pageSizeStr := ctx.URLParamDefault("page_size", "10")

	page, err := strconv.ParseInt(pageStr, 10, 64)
	if err != nil {
		page = 1
	}

	pageSize, err := strconv.ParseInt(pageSizeStr, 10, 64)
	if err != nil {
		pageSize = 10
	}

	result, err := dy.douyinService.GetVideos(page, pageSize)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, result)
}

func (dy *Douyin) GetVideosByProfile(ctx iris.Context) {
	profileID := ctx.Params().Get("profile_id")
	if profileID == "" {
		dy.Error(ctx, ErrParams.Code, "Profile ID is required", nil)
		return
	}

	pageStr := ctx.URLParamDefault("page", "1")
	pageSizeStr := ctx.URLParamDefault("page_size", "10")

	page, err := strconv.ParseInt(pageStr, 10, 64)
	if err != nil {
		page = 1
	}

	pageSize, err := strconv.ParseInt(pageSizeStr, 10, 64)
	if err != nil {
		pageSize = 10
	}

	result, err := dy.douyinService.GetVideosByProfile(profileID, page, pageSize)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, result)
}

func (dy *Douyin) UpdateVideo(ctx iris.Context) {
	id := ctx.Params().Get("id")
	if id == "" {
		dy.Error(ctx, ErrParams.Code, "Video ID is required", nil)
		return
	}

	var req douyinService.UpdateVideoRequest
	if err := ctx.ReadJSON(&req); err != nil {
		dy.Error(ctx, ErrParams.Code, ErrParams.Message, err)
		return
	}

	video, err := dy.douyinService.UpdateVideo(id, &req)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, video)
}

func (dy *Douyin) DeleteVideo(ctx iris.Context) {
	id := ctx.Params().Get("id")
	if id == "" {
		dy.Error(ctx, ErrParams.Code, "Video ID is required", nil)
		return
	}

	err := dy.douyinService.DeleteVideo(id)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, common.RspProto{
		Code:    OK.Code,
		Message: "Video deleted successfully",
	})
}

// User APIs
func (dy *Douyin) SaveUserInfo(ctx iris.Context) {
	profileID := ctx.Params().Get("profile_id")
	if profileID == "" {
		dy.Error(ctx, ErrParams.Code, "Profile ID is required", nil)
		return
	}

	var req douyinService.SaveUserInfoRequest
	if err := ctx.ReadJSON(&req); err != nil {
		dy.Error(ctx, ErrParams.Code, ErrParams.Message, err)
		return
	}

	user, err := dy.douyinService.SaveUserInfo(profileID, &req)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, user)
}

func (dy *Douyin) GetUserInfo(ctx iris.Context) {
	profileID := ctx.Params().Get("profile_id")
	if profileID == "" {
		dy.Error(ctx, ErrParams.Code, "Profile ID is required", nil)
		return
	}

	user, err := dy.douyinService.GetUserInfo(profileID)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, user)
}

// Stats API
func (dy *Douyin) GetStats(ctx iris.Context) {
	stats, err := dy.douyinService.GetStats()
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, stats)
}

// Video List APIs
func (dy *Douyin) GetVideoList(ctx iris.Context) {
	var req douyinService.VideoListRequest
	
	// Parse query parameters and JSON body
	if ctx.Method() == "POST" {
		if err := ctx.ReadJSON(&req); err != nil {
			dy.Error(ctx, ErrParams.Code, ErrParams.Message, err)
			return
		}
	} else {
		// Parse query parameters for GET request
		if profileID := ctx.URLParam("profile_id"); profileID != "" {
			req.ProfileID = &profileID
		}
		if platform := ctx.URLParam("platform"); platform != "" {
			req.Platform = &platform
		}
		if status := ctx.URLParam("download_status"); status != "" {
			req.DownloadStatus = &status
		}
		if author := ctx.URLParam("author"); author != "" {
			req.Author = &author
		}
		if keywords := ctx.URLParam("keywords"); keywords != "" {
			req.Keywords = &keywords
		}
		if startDate := ctx.URLParam("start_date"); startDate != "" {
			req.StartDate = &startDate
		}
		if endDate := ctx.URLParam("end_date"); endDate != "" {
			req.EndDate = &endDate
		}
		if sortBy := ctx.URLParam("sort_by"); sortBy != "" {
			req.SortBy = &sortBy
		}
		if sortOrder := ctx.URLParam("sort_order"); sortOrder != "" {
			req.SortOrder = &sortOrder
		}
	}

	pageStr := ctx.URLParamDefault("page", "1")
	pageSizeStr := ctx.URLParamDefault("page_size", "10")

	page, err := strconv.ParseInt(pageStr, 10, 64)
	if err != nil {
		page = 1
	}

	pageSize, err := strconv.ParseInt(pageSizeStr, 10, 64)
	if err != nil {
		pageSize = 10
	}

	result, err := dy.douyinService.GetVideoList(&req, page, pageSize)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, result)
}

func (dy *Douyin) BatchUpdateVideoStatus(ctx iris.Context) {
	var req douyinService.BatchUpdateVideoStatusRequest
	if err := ctx.ReadJSON(&req); err != nil {
		dy.Error(ctx, ErrParams.Code, ErrParams.Message, err)
		return
	}

	err := dy.douyinService.BatchUpdateVideoStatus(&req)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, common.RspProto{
		Code:    OK.Code,
		Message: "Videos status updated successfully",
	})
}

func (dy *Douyin) BatchDeleteVideos(ctx iris.Context) {
	var req douyinService.BatchVideoRequest
	if err := ctx.ReadJSON(&req); err != nil {
		dy.Error(ctx, ErrParams.Code, ErrParams.Message, err)
		return
	}

	err := dy.douyinService.BatchDeleteVideos(&req)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, common.RspProto{
		Code:    OK.Code,
		Message: "Videos deleted successfully",
	})
}

func (dy *Douyin) GetVideoStatistics(ctx iris.Context) {
	var req douyinService.VideoStatsRequest
	
	// Parse query parameters
	if profileID := ctx.URLParam("profile_id"); profileID != "" {
		req.ProfileID = &profileID
	}
	if platform := ctx.URLParam("platform"); platform != "" {
		req.Platform = &platform
	}
	if startDate := ctx.URLParam("start_date"); startDate != "" {
		req.StartDate = &startDate
	}
	if endDate := ctx.URLParam("end_date"); endDate != "" {
		req.EndDate = &endDate
	}

	result, err := dy.douyinService.GetVideoStatistics(&req)
	if err != nil {
		dy.Error(ctx, ErrParams.Code, err.Error(), err)
		return
	}

	dy.Success(ctx, result)
}

// Legacy method (keep for backward compatibility)
func (dy *Douyin) AddHomePage(c iris.Context) {
	dy.CreateProfile(c)
}
